package com.baobaoyeye.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baobaoyeye.conn.ConnMySQL;

/**
 * Servlet implementation class Addcounts
 */
public class Addcounts extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ConnMySQL connSql = null;   
    private ResultSet rs = null;
    private String _user_id = null;
    private String _type = null;
	private String subid = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Addcounts() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		connSql = new ConnMySQL();
		// TODO Auto-generated method stub
		String studentid = null;
		String count = null;
		PrintWriter out = response.getWriter();
		String tmpstudentid = "studentid";
		String tmpcount ="count";
		_user_id = request.getParameter("_user_id");
		_type = request.getParameter("_type");
		//out.println(_user_id);
		//out.println(_type);
		int num = 1;
		boolean flag = false;
		while((studentid=request.getParameter(tmpstudentid))!=null && (count=request.getParameter(tmpcount))!=null){
			if(ISStu_Sub(studentid)){
				//out.println("ok");
				if(Upload(studentid,count));
			}
			
			tmpstudentid = "studentid" + num;
			tmpcount = "count" + num;
			//out.println(tmpstudentid + "  "+tmpcount );
			num++;
		}
		response.setContentType("text/html");
		HttpSession session =request.getSession(true);
		if(_user_id!=null && _user_id.length()>0){
			session.setAttribute("_type",_type);
			session.setAttribute("_user_id",_user_id);
		}
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		String newLocn = "seeallcounts.jsp";
		response.setHeader("Location", newLocn);
			
		
		
	}

	private boolean Upload(String studentid, String count) {
		// TODO Auto-generated method stub
		
		String sql = "update sub_stu set count='"+count+"' where stu_id='"+studentid+"' and sub_id='"+subid+"';";
		//System.out.println("sql--------------"+sql);
		if(Integer.parseInt(count) < 0 || Integer.parseInt(count) > 100)
			return false;
		int ok = connSql.executeUpdate(sql);
		if(ok <= 0){
			return false;
		}
		return true;
	}

	private boolean ISStu_Sub(String studentid) {
		subid = WhichSub();
		if(subid!=null){
			String sql = "select stu_id from sub_stu where sub_id='"+subid+"';";
			//System.out.println("sql-----------"+sql);
			rs = connSql.executeQuery(sql);
			try {
				while(rs.next()){
					String db_stu_id = rs.getString(1);
					//System.out.println("stu_id-----------------"+db_stu_id);
					//System.out.println("studentid=============="+studentid);
					if(db_stu_id.equals(studentid) || db_stu_id == studentid){
						return true;
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				connSql.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		return false;
	}

	private String WhichSub() {
		
		String sql = "select subject_id from subject where subject_teacher_id='"+_user_id+"';";
		rs = connSql.executeQuery(sql);
		try {
			if(rs.next()){
				return rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
